﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Net;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Classwork10
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private async void button1_Click(object sender, EventArgs e)
        {
            string str1 = await WebSearch("http://www.baidu.com/s?wd=" + textBox1.Text);
            string str2 = await WebSearch("https://cn.bing.com/search?q=" + textBox1.Text);

            string x = @"[\u4E00-\u9FFF]+";
            MatchCollection mc1 = Regex.Matches(str1, x, RegexOptions.IgnoreCase);
            MatchCollection mc2 = Regex.Matches(str2, x, RegexOptions.IgnoreCase);
            StringBuilder sb1 = new StringBuilder();
            StringBuilder sb2 = new StringBuilder();
            foreach (Match NextMach in mc1)
            {
                sb1.Append(NextMach.Value);
                sb1.Append("\r\n");
            }
            foreach (Match NextMach in mc2)
            {
                sb2.Append(NextMach.Value);
                sb2.Append("\r\n");
            }
            textBox2.Text = sb1.ToString();
            textBox3.Text = sb2.ToString();
        }

        private Task<string>WebSearch(string url)
        {
            return Task<string>.Run(() =>
            {
                WebClient client = new WebClient();
                client.Encoding = Encoding.UTF8;
                string str = client.DownloadString(url);
                Console.WriteLine(str);
                return str;
            });
        }
    }
}
